//---------------Sonic CD BrickBlock Script---------------//
//-------Scripted by makasinaaaa & Christian Whitehead--------//

// Aliases
#alias Object.Value0	:	Object.Timer
#alias Object.Value1	:	Object.XVelocity
#alias Object.Value2	:	Object.YVelocity
#alias Object.Value3	:	Object.GoTimer
#alias Object[24].PropertyValue	:	HUD.CurrentTimePeriod

// Global SFX
#alias 22	:	SFX_G_EXPLOSION
#alias 1	:	GRAVITY_AIR

// Property Values
#alias 0	:	Block
#alias 1	:	VARIANT_1_BIG_LEFT
#alias 2	:	VARIANT_1_BIG_MIDDLE
#alias 3	:	VARIANT_1_BIG_RIGHT

// from 8 onwards it will be considered debris


sub ObjectMain
	if Object.PropertyValue > 7
		Object.XPos += Object.XVelocity
		Object.YPos += Object.YVelocity
		Object.YVelocity += 0x4000
		if Object.OutOfBounds == true
			Object.Type = TypeName[Blank Object]
		end if
	end if
end sub


sub ObjectPlayerInteraction
	
	switch Object.PropertyValue
	case 0
			PlayerObjectCollision(C_BOX, -16, -16, 16, 16)
		break
	case 1
		if Player.Animation == ANI_JUMPING
			PlayerObjectCollision(C_TOUCH, -16, -16, 16, 16)
			if CheckResult == true
				Player.YVelocity = -0x30000
				Player.Gravity = GRAVITY_AIR
				Object.Type = TypeName[Blank Object]
				PlaySfx(SFX_G_EXPLOSION, false) 
				TempValue0  = Object.PropertyValue
				TempValue0 *= 6
				TempValue0 += 8

				TempValue1  = Object.XPos
				TempValue1 -= 0x80000

				TempValue2  = Object.XPos
				TempValue2 += 0x80000

				TempValue3  = Object.YPos
				TempValue3 -= 0x100000

				CreateTempObject(TypeName[BrickBlock], TempValue0, TempValue1, TempValue3)
				Object[TempObjectPos].YVelocity = -0x20000
				Object[TempObjectPos].XVelocity = -0x20000
				Object[TempObjectPos].DrawOrder =  4
				TempValue0++

				CreateTempObject(TypeName[BrickBlock], TempValue0, TempValue2, TempValue3)
				Object[TempObjectPos].YVelocity = -0x20000
				Object[TempObjectPos].XVelocity = 0x20000
				Object[TempObjectPos].DrawOrder =  4
				TempValue0++

				TempValue3 += 0x100000
				
				CreateTempObject(TypeName[BrickBlock], TempValue0, TempValue1, TempValue3)
				Object[TempObjectPos].YVelocity = -0x10000
				Object[TempObjectPos].XVelocity = -0x10000
				Object[TempObjectPos].DrawOrder =  4
				TempValue0++

				CreateTempObject(TypeName[BrickBlock], TempValue0, TempValue2, TempValue3)
				Object[TempObjectPos].YVelocity = -0x10000
				Object[TempObjectPos].XVelocity = 0x10000
				Object[TempObjectPos].DrawOrder =  4
				TempValue0++

				
				TempValue3 += 0x100000
			end if
		else
			PlayerObjectCollision(C_BOX, -16, -16, 16, 16)
		end if
		break
	case 2
		PlayerObjectCollision(C_BOX, -16, -16, 16, 16)
			PlayerObjectCollision(C_TOUCH, -16, -17, 16, -16)
			if CheckResult == true
				Object.GoTimer = 1
			end if
			if Object.GoTimer == 1
				Object.Timer++
				if Object.Timer == 6
					Object.Type = TypeName[Blank Object]
					PlaySfx(SFX_G_EXPLOSION, false) 
					TempValue0  = Object.PropertyValue
					TempValue0 *= 6
					TempValue0 += 8

					TempValue1  = Object.XPos
					TempValue1 -= 0x80000

					TempValue2  = Object.XPos
					TempValue2 += 0x80000

					TempValue3  = Object.YPos
					TempValue3 -= 0x100000

					CreateTempObject(TypeName[BrickBlock], TempValue0, TempValue1, TempValue3)
					Object[TempObjectPos].YVelocity = 0x20000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					CreateTempObject(TypeName[BrickBlock], TempValue0, TempValue2, TempValue3)
					Object[TempObjectPos].YVelocity = 0x20000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					TempValue3 += 0x100000
					
					CreateTempObject(TypeName[BrickBlock], TempValue0, TempValue1, TempValue3)
					Object[TempObjectPos].YVelocity = 0x20000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					CreateTempObject(TypeName[BrickBlock], TempValue0, TempValue2, TempValue3)
					Object[TempObjectPos].YVelocity = 0x20000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					
					TempValue3 += 0x100000
				end if
			end if
		break
	end switch
end sub



sub ObjectDraw
	DrawSprite(0)
	if Object.PropertyValue == 0
		DrawSprite(1)
	else
	end if
	if Object.PropertyValue == 1
		DrawSprite(1)
	else
	end if
	if Object.PropertyValue == 2
		DrawSprite(1)
	else
	end if
end sub


sub ObjectStartup
	LoadSpriteSheet("R2/Objects2.gif")

	// Variant 1
	TempValue0 = HUD.CurrentTimePeriod
	TempValue1 = 1
	TempValue0 *= 33
	TempValue1 += TempValue0 
	SpriteFrame(-8, -8, 16, 16, TempValue1, 66)			// #0 - Destoed Block
	SpriteFrame(-16, -16, 32, 32, TempValue1, 66)		// #1 - BrickBlock
	
	// Variant 1 debris chunks

	// Debris chunks BreakWall singular piece
	TempValue0 = 0
	TempValue3 = 1
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 190
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall left piece
	TempValue0 = 0
	TempValue3 = 1
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 223
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall middle piece
	TempValue0 = 0
	TempValue3 = 50
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 190
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall right piece
	TempValue0 = 0
	TempValue3 = 50
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 223
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop
	// original code have this below LoadSpriteSheet, moved for consistency with the rest
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[BrickBlock]
			Object[ArrayPos0].DrawOrder = 4
		end if
		ArrayPos0++
	loop
end sub

